package com.base.balibrary.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.base.balibrary.R;
import com.base.balibrary.utils.ESManager.ExecutorServiceManager;
import com.base.balibrary.utils.ViewInjectUtil;


public class BaseActivity extends Activity implements View.OnClickListener{

    public static BaseApplication mApplication;

    //访问网络状态
    protected static final short STATUS_OK=100;
    protected static final short STATUS_CHECK=101;
    protected static final short STATUS_UPDATE=102;
    protected static final short STATUS_CLOSE=103;
    protected static final short STATUS_ERROR=104;
    protected static final short STATUS_MORE=105;

    //储存activity列表
    private static List<Activity> activities;

    //主要请求线程池
    protected ExecutorService mThreadPool;

    //获得屏幕信息
    public static WindowManager windowManager;
    public static int MOBILE_WIDTH;
    public static int MOBILE_HEIGHT;

    //快捷返回控制器
    private boolean isLeftMove=false;
    //可响应触摸宽度
    int move=(int)(MOBILE_WIDTH*0.12);
    //按下位置
    float downX=0f;
    //抬起位置
    float upX=0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(mApplication==null){
            mApplication=(BaseApplication)getApplication();
        }

        //创建缓存线程池
        if(mThreadPool==null){
            mThreadPool= ExecutorServiceManager.getInstance().newFixedThreadPool();
        }

        if(windowManager==null){
            windowManager=getWindowManager();
            // 获取屏幕宽高
            if (MOBILE_WIDTH == 0 && MOBILE_HEIGHT == 0) {
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                MOBILE_WIDTH = dm.widthPixels;
                MOBILE_HEIGHT = dm.heightPixels;
            }
        }

        //activity Stack 列表
        if(activities==null){
            activities=new ArrayList<Activity>();
        }
        //添加activity
        addActivity(this);
        //界面注解注入
        ViewInjectUtil.inject(this);
        //是否快速返回
        setIsLeftMove(false);
    }

    /**
     * 添加activity
     * @param activity
     */
    public void addActivity(Activity activity){
        activities.add(activity);
    }

    /**
     * 退出程序
     */
    public void exitServer(){
        for(Activity activity : activities){
            if(activity!=null){
                activity.finish();
            }
        }
        System.exit(0);
    }

    /**
     * activity跳转
     * 默认进入跳转
     * toActivity(context,cls,false,false);
     * @param context
     * @param cls
     */
    protected void strActivity(Context context, Class<?> cls){
        strActivity(context, cls, false, false);
    }

    /**
     * 快速跳转
     * 先更改跳转样式在finish
     * @param context
     * @param cls
     * @param closeActivity 是否关闭当前activity
     * @param isOut false 转入   true 转出
     */
    protected void strActivity(Context context,Class<?> cls,Boolean closeActivity,Boolean isOut){
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        setGo(isOut);
        if (closeActivity) {
            finish();
        }
    }

    /**
     * 传参数快速跳转(单参数)
     * @param context
     * @param cls
     * @param closeActivity
     * @param isOut
     * @param flg 参数 接收默认为 "flg" 不可修改
     */
    protected void strActivity(Context context, Class<?> cls,
                             Boolean closeActivity, Boolean isOut, String flg) {
        strActivity(context, cls, closeActivity, isOut, flg, "", "");
    }

    /**
     * 传参跳转(双参数)
     * @param context
     * @param cls
     * @param closeActivity
     * @param isOut
     * @param flg 第一个参数，接收为 "flg"
     * @param flg2Name 第二个参数接收名称,名称不能为空否则不传
     * @param flg2Value 第二个参数
     */
    protected void strActivity(Context context, Class<?> cls,
                             Boolean closeActivity, Boolean isOut, String flg, String flg2Name,
                             String flg2Value) {
        Intent intent = new Intent(context, cls);
        intent.putExtra("flg", flg);
        if(!flg2Name.equals("")) {
            intent.putExtra(flg2Name, flg2Value);
        }
        context.startActivity(intent);
        if (closeActivity) {
            finish();
        }
        setGo(isOut);
    }

    /**
     * 带跳转样式的关闭activity
     */
    protected void finishActivity(){
        finish();
        setGo(true);
    }

    /**
     * 定义了activity的跳转样式
     */
    protected void setGo(boolean isOut) {
        // 设置默认值
        int inEnter = R.anim.zoomin;
        int inExit = R.anim.zoomout;
        int outEnter = R.anim.backin;
        int outExit = R.anim.backout;

        if (!isOut) {
            overridePendingTransition(inEnter, inExit);
        } else {
            overridePendingTransition(outEnter, outExit);
        }
    }

    /**
     * 快速提示
     * 时长 LENGTH_LONG
     * @param context
     * @param str
     */
    protected void toastMessage(Context context, String str) {
        toastMessage(context,str,Toast.LENGTH_LONG);
    }

    /**
     * 快速提示
     * @param context
     * @param str
     * @param time 提示时长
     */
    protected void toastMessage(Context context,String str,int time){
        Toast.makeText(context, str, time).show();
    }

    protected void toastMessageNoNet(Context context) {
        toastMessage(context, "请检查网络连接");
    }

    /**
     * 网络判断
     * @param context
     * @return
     */
    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        toastMessageNoNet(context);
        return false;
    }

    /**
     * 返回按钮finish activity
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {

    }

    public void setIsLeftMove(boolean isLeftMove){
        this.isLeftMove=isLeftMove;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(!isLeftMove)
            return super.onTouchEvent(event);
        final int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:{
                downX=event.getX();
                break;}
            case MotionEvent.ACTION_MOVE:{
                break;}
            case MotionEvent.ACTION_UP:{
                upX=event.getX();
                if(downX<=move){
                    if(upX-downX>0){
                        finishActivity();
                        return true;
                    }
                }
                break;}

        }
        return super.onTouchEvent(event);
    }


}
